package com.accp.dao.impl;

import com.accp.dao.PersonDao;
import com.accp.pojo.Person;
import com.accp.pojo.Users;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;

import javax.annotation.Resource;
import java.util.List;

public class PersonDaoImpl extends BaseDaoImpl implements PersonDao {


    @Override
    public boolean add(Person person) {
        boolean flag = false;
        try{
            getSession().save(person);
            flag = true;
        }catch(HibernateException e){
            e.printStackTrace();
        }
        return flag;
    }


    @Override
    public boolean update(Person person) {
        boolean flag = false;
        try{
            getSession().update(person);
            flag = true;
        }catch(HibernateException e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 根据id删除
     * @param pid
     * @return
     */
    @Override
    public boolean delById(Integer pid) {
        boolean flag = false;
        try {
            String hql="delete Person as p where p.pid=?";
           //此处使用标准语句同样适用:"delete from Person where id=?";
            //同样select和update都适用
             Query query=getSession().createQuery(hql);
             query.setInteger(0,pid);
             query.executeUpdate();
             getSession().beginTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 根据对象Person删除
     * @param person
     * @return
     */
    @Override
    public boolean deletePerson(Person person) {
        boolean flag = false;
        try{
            getSession().delete(person);
            flag = true;
        }catch(HibernateException e){
            e.printStackTrace();
        }
        return flag;

    }

    @Override
    public List<Person> getAll(String className) {
        //List<Person> list = new ArrayList<Person>();
        Query query = this.getSession().createQuery("from " + className);
        return query.list();
    }

    @Override
    public Person findById(Integer pid) {
        //return personDao.findById(pid);
        Person person = null;
        try {
            person = (Person) getSession().get(Person.class, new Integer(pid));
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return person;

    }
    public int totalPage(String className) {
        String hql = "select count(*) from "+ className;
        Query query = this.getSession().createQuery(hql);
        int total = Integer.parseInt(query.list().get(0).toString());
        return total;
    }
    /**
     * 分页查询，查询首页
     */
    @SuppressWarnings("unchecked")
    public List<Person> selectByPage(String className, int pageNo, int pageSize) {
        Query query = this.getSession().createQuery("from "+ className);
        query.setFirstResult((pageNo - 1) * pageSize);//每页显示的第一条记录
        query.setMaxResults(pageSize);//每页显示的记录数
        return query.list();
    }
}
